package com.qm.console.controller.rest;

import com.google.common.collect.ImmutableMap;
import com.qm.console.component.exception.BusinessException;
import com.qm.console.controller.dto.BaseRsp;
import com.qm.console.controller.dto.ResultEnum;
import com.qm.console.pubdef.jw.dto.ProductQueryDTO;
import com.qm.console.service.jw.JWResourceService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * //                       _oo0oo_
 * //                      o8888888o
 * //                      88" . "88
 * //                      (| -_- |)
 * //                      0\  =  /0
 * //                    ___/`---'\___
 * //                  .' \\|     |// '.
 * //                 / \\|||  :  |||// \
 * //                / _||||| -:- |||||- \
 * //               |   | \\\  -  /// |   |
 * //               | \_|  ''\---/''  |_/ |
 * //               \  .-\__  '-'  ___/-. /
 * //             ___'. .'  /--.--\  `. .'___
 * //          ."" '<  `.___\_<|>_/___.' >' "".
 * //         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 * //         \  \ `_.   \_ __\ /__ _/   .-` /  /
 * //     =====`-.____`.___ \_____/___.-`___.-'=====
 * //                       `=---='
 * //
 * //
 * //     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * //
 * //               佛祖保佑         永无BUG
 * <p>
 * com.qm.console.controller.rest
 * Created on 2021/7/22.
 */
@RestController
@RequestMapping("/upload")
public class JwResourceController {

   @Value("${spring.uploadFolder}")
   private String destFolder;
   @Autowired
   private JWResourceService jwResourceService;

   private File preprocessUpload(MultipartFile file) {
      if (file.isEmpty()) {
         throw new BusinessException(ResultEnum.UPLOAD_FAILED_NOT_FOUND);
      }

      String fileName = file.getOriginalFilename();
      if (StringUtils.isEmpty(fileName)) {
         throw new BusinessException(ResultEnum.UPLOAD_FAILED_NOT_FOUND);
      }
      fileName = System.currentTimeMillis() + "." + fileName;
      File destDir = new File(destFolder);
      if (!destDir.isDirectory()) {
         destDir.mkdirs();
      }
      File dest = new File(destFolder, fileName);
      try {
         file.transferTo(dest);
      } catch (IOException e) {
         throw new BusinessException(ResultEnum.UPLOAD_FAILED_IO_EXCEPTION);
      }
      return dest;
   }

   @PostMapping("image")
   public BaseRsp uploadImage(@RequestParam("file") MultipartFile file) {
         return new BaseRsp(ResultEnum.SUCCESS, jwResourceService.processImageUpload(preprocessUpload(file)));
   }

   @PostMapping("video")
   public BaseRsp uploadVideo(@RequestParam("file") MultipartFile file) {
      return new BaseRsp(ResultEnum.SUCCESS, jwResourceService.processVideoUpload(preprocessUpload(file)));
   }

   @PostMapping("products")
   public BaseRsp uploadProducts(@RequestParam("file") MultipartFile file) {
      jwResourceService.processUpload(preprocessUpload(file));
      return new BaseRsp(ResultEnum.SUCCESS);

   }

   @PutMapping("product/reset/{id}")
   public BaseRsp resetProductState(@PathVariable("id") Integer id) {
         jwResourceService.retryProductUpload(id);
         return new BaseRsp(ResultEnum.SUCCESS);
   }

   @GetMapping
   public BaseRsp listProducts(ProductQueryDTO query) {
//      jwResourceService.listProducts(query);
      return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("list", jwResourceService.listProducts(query),
              "total", jwResourceService.countProducts(query)));
   }
}
